package com.comtrade.map;

import java.util.Random;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.example.beaconapk.R;

public class MapFrame extends FrameLayout {

	private Context mContext;
	private LayoutInflater layoutInflater;
	
	protected DotView dot;
	protected TouchView touchView;

	/**
	 * Button on mapFrame
	 */
	private Button btnFindMe;

	public MapFrame(Context context) {
		super(context);
		this.mContext = context;
		inflate();
		bindViews();
		
	}

	private void inflate() {
		layoutInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.map_frame, this, true);
	}

	private void bindViews() {
		// bind all views here
		touchView = (TouchView) findViewById(R.id.touch_view);
		dot = touchView.getDot();
		dot.setCoordinates(0, 0);
		btnFindMe = (Button) findViewById(R.id.btnFindMe);
		
		btnFindMe.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Random r = new Random();
				int i = r.nextInt(5)+1;
				dot.setCoordinates(i*100, i*100);
				
			}
		});
	}
	
	public DotView getDotView(){
		return dot;
	} 
	
	public TouchView getTouchView(){
		return touchView;
	}
	
	public Button getButton(){
		return btnFindMe;
	}
	
	/**
	 * sets Drawable mapImage for map image with coordinates Rect(0,0,w,h);
	 * @param mapImage
	 * @param w
	 * @param h
	 */
	
	public void setMapImage(Drawable mapImage, int w, int h) {
		if (mapImage != null){
			touchView.setMap(mapImage, w, h);
		}
		else {
			Log.e(VIEW_LOG_TAG, "map is null");
		}
	}
}
